<?php

namespace App\Model;

use Illuminate\Database\Eloquent\Model;

class AdminUserModel extends Model
{
    //
    protected $table = 'shop_admin_users';

    public function roles()
    {
        return $this->belongsToMany('App\Model\AdminRoleModel', 'shop_admin_user_roles', 'user_id', 'role_id');
    }

    public static function hasNode($node)
    {
        $user = session('Adminuser-data');
        $node_role = [];
        foreach (\App\Model\AdminUserModel::find($user['id'])->roles as $role) {
            foreach ($role->nodes->toArray() as $value) {
                $node_role[] = $value['name'];
            }
        }
        $node_role = array_unique($node_role);

        if (in_array($node, $node_role)) {
            return true;
        }

        return false;
    }

    public static function hasRole($role)
    {
        $user = session('Adminuser-data');
        $role_user = [];
        foreach (\App\Model\AdminUserModel::find($user['id'])->roles->toArray() as $value) {

                $role_user[] = $value['name'];

        }
        if (array_intersect($role, $role_user)) {

            return true;

        }

        return false;
    }
}
